<?php
/**
 * @copyright 
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */
namespace app\admin\model;
use think\facade\Db;
use think\model;
use app\common\controller\WeChat;
class Device extends Model
{
    /**
    * 获取分页列表
    * @param $where
    * @param $param
    */
    public function getDeviceList($where, $param)
    {
		$rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit'];
		$order = empty($param['order']) ? 'id desc' : $param['order'];
    $list = self::where($where)->order($order)->paginate($rows, false, ['query' => $param]);
    $list = $list->toArray();
        foreach($list['data'] as &$v)
        {
              if (!empty($v['user_one'])) {
                  $user_one = Db::name('user_one')->where('id',$v['user_one'])->find();
                  if (!empty($user_one['pid'])) {
                      $user_two_title = Db::name('user_two')->where('id',$user_one['pid'])->value('user_name');
                      $v['user_two_title'] = $user_two_title.'(ID：'.$user_one['pid'].')';
                  } else {
                      $v['user_two_title'] = '';
                  }
                  $v['user_one_title'] = $user_one['user_name'].'(ID：'.$v['user_one'].')';
              } else {
                  $v['user_one_title'] = '';
                  $v['user_two_title'] = '';
              }
            if (!empty($v['user_three'])) {
                $user_three_title = Db::name('user_three')->where('id',$v['user_three'])->value('user_name');
                $v['user_three_title'] = $user_three_title.'(ID：'.$v['user_three'].')';
            } else {
                $v['user_three_title'] = '';
            }
            $v['wine_num'] = Db::name('order')
                ->where([['status','>',0],['delete_time','=',0],['device_id','=',$v['id']]])->sum('wine_num');
        }
		return $list;
    }

    /**
    * 添加数据
    * @param $param
    */
    public function addDevice($param)
    {
		$insertId = 0;
        try {
			$param['create_time'] = time();
			$insertId = self::strict(false)->field(true)->insertGetId($param);
			add_log('add', $insertId, $param);
        } catch(\Exception $e) {
			return to_assign(1, '操作失败，原因：'.$e->getMessage());
        }
		return to_assign(0,'操作成功',['aid'=>$insertId]);
    }

    /**
    * 编辑信息
    * @param $param
    */
    public function editDevice($param)
    {
        try {
            $param['update_time'] = time();
            $info = Db::name('device')->where('id',$param['id'])->find();
            if (empty($info['wx_code'])) {
                $wechat = new WeChat();
                $param['wx_code'] = $wechat->getQRCode($param['id']);
            }
            self::where('id', $param['id'])->strict(false)->field(true)->update($param);
			add_log('edit', $param['id'], $param);
        } catch(\Exception $e) {
			return to_assign(1, '操作失败，原因：'.$e->getMessage());
        }
		return to_assign();
    }
	

    /**
    * 根据id获取信息
    * @param $id
    */
    public function getDeviceById($id)
    {
        $info = self::where('id', $id)->find();
        if(isset($info['image'])&&$info['image'])
        {
          $info['image'] = cdnurl($info['image']);
        }
		return $info;
    }

    /**
    * 删除信息
    * @param $id
    * @return array
    */
    public function delDeviceById($id,$type=0)
    {
		if($type==0){
			//逻辑删除
			try {
				$param['delete_time'] = time();
				self::where('id', $id)->update(['delete_time'=>time()]);
				add_log('delete', $id);
			} catch(\Exception $e) {
				return to_assign(1, '操作失败，原因：'.$e->getMessage());
			}
		}
		else{
			//物理删除
			try {
				self::where('id', $id)->delete();
				add_log('delete', $id);
			} catch(\Exception $e) {
				return to_assign(1, '操作失败，原因：'.$e->getMessage());
			}
		}
		return to_assign();
    }
}

